<?php
namespace app\admin\controller;
use database\library\Backup;
use think\Cache;
use think\Db;
use think\Request;


/**
 * 操作记录类
 *
 * Class OperationController
 * @package Admin\Controller
 */
class Operation extends Admin {

    /**
     * 显示操作记录
     */
    public function showlist(Request $request)
    {
        $begin_time = $request->get('from');
        $end_time   = $request->get('to');
        $where = [];
        if ($begin_time || $end_time){
            if ($begin_time){
                $where['operation_time'] = array('>',$begin_time);

                $this ->assign('begin_time',$begin_time);
            }
            if ($end_time){
                $where['operation_time'] = array('<',$end_time);
                $this ->assign('end_time',$end_time);
            }
        }
        $log_list = Db::name('operation_log')
            ->alias('o')
            ->field('o.*,u.username')
            ->join('dl_user u','u.id = o.operation_uid')
            ->order('o.id desc')
            ->paginate(30);

        $list = $log_list->all();
        foreach ( $list as $k=>$v){
            $cz = strtolower('admin/'.	$v['operation_node']);
            $do_name =  Db::name('auth_rule')
                ->where('name',$cz)
                ->value('title');
            if(!$do_name){
                $do_name = '未可知';
            }
            $v['do_name'] = $do_name;
            $log_list[$k] = $v;
        }
        return view('showlist',[
            'log_list'=>$log_list,
            'page'=>$log_list->render(),
        ]);

    }


    /**
     * 软件参数设置
     */
    public function index(){
        $data = Db::name('setting')->select()[0];
        return view('',[
            'data'=>$data,
        ]);
    }

    /**
     * 修改系统参数
     */
    public function edit(Request $request){
        if($request->isPost()){
            $data = $request->post();
            $map=array(
                'id'=>$data['id']
            );
            $result=Db::name('setting')->where($map)->update($data);
            if ($result) {
                $this->success_new('修改成功',url('Admin/operation/index'));
            }else{
                $this->error_new('修改失败');
            }
        }
    }

    /**
     * 根据IP获取地址
     */
    public function get_addr(Request $request){
       $ip   = $request->param('ip');
       $addr = get_city_id($ip);
       Cache::set($ip,$addr);
       $data['operation_addr'] = $addr;
       $result = Db::name('operation_log')
           ->where(array('operation_ip'=>$ip,'operation_addr'=>0))
           ->update($data);
        if ($result) {
            $this->success_new('获取成功',url('Admin/operation/showlist'));
        }else{
            $this->error_new('获取失败');
        }
    }

    /**
     * 删除显示
     */
    public function delete_show(){
        return view('',[

        ]);
    }

    /**
     * 删除数据
     */
    public function do_delete(Request $request){
       $id  = $request->post('id');
       $day = $request->post('day');
       if($id==4){
           $del_day = "-".$day." day";
           $del_time = date("Y-m-d",strtotime($del_day));
           for($i=1;$i<=10;$i++){
               switch ($i){
                   case 1:
                       $table_name = "at_eight";
                       break;
                   case 2:
                       $table_name = "at_canada";
                       break;
                   case 3:
                       $table_name = "at_car";
                       break;
                   case 4:
                       $table_name = "at_airship";
                       break;
                   case 5:
                       $table_name = "at_ssc";
                       break;
                   case 6:
                       $table_name = "at_tjssc";
                       break;
                   case 7:
                       $table_name = "at_gd10";
                       break;
                   case 8:
                       $table_name = "at_cq10";
                       break;
                   case 9:
                       $table_name = "at_fast";
                       break;
                   case 10:
                       $table_name = "at_gd11";
                       break;
                   case 11:
                       $table_name = "at_hk";
                       break;
               }
               Db::name($table_name)->where('create_time','<',$del_time)->delete();
           }

       }

        if($id==3){
            $del_day = "-".$day." day";
            $del_time = date("Y-m-d",strtotime($del_day));
            $table_name = "operation_log";
            Db::name($table_name)->whereTime('operation_time','<',$del_time)->delete();

        }
        if(in_array($id,[1,2,5,6,7])) {
            $del_day = "-".$day." day";
            $del_time =strtotime($del_day);
            if ($id == 1) {
                $table_name = "member";
            }
            if ($id == 2) {
                $table_name = "login_log";
            }
            if ($id == 5) {
                $table_name = "recharge";
            }
            if ($id == 6) {
                $table_name = "withdrawals";
            }
            if ($id == 7) {
                $table_name = "single";
            }
            Db::name($table_name)->where('create_at', '<', $del_time)->delete();
        }
        json_return(200,'删除成功');
    }

    /**
     * 二维码
     */
    public function qrcode(Request $request){
      $type = $request->param('type');
      $w = [];
      if($type){
          $w['type'] = $type;
      }
      $data = Db::name('qrcode')
          ->where($w)
          ->order('id desc')
          ->select();
        return view('',[
            'data'=>$data
        ]);
    }

    /**
     * 编辑二维码
     */
    public function qrcode_show(Request $request){
        $id = $request->param('id');
        $data = Db::name('qrcode')
            ->where(array('id'=>$id))
            ->find();
        return view('qrcode_edit',[
            'data'=>$data
        ]);
    }

    /**
     * 编辑二维码
     */
    public function qrcode_edit(Request $request){
        if($request->isPost()){
            $data = $request->post();
            $map=array(
                'id'=>$data['id']
            );
            $result=Db::name('qrcode')->where($map)->update($data);
            if ($result) {
                $this->success_new('修改成功',url('operation/qrcode'));
            }else{
                $this->error_new('修改失败');
            }
        }
    }

    /**
     * 删除二维码
     */
    public function qrcode_del(Request $request){
        $id = $request->param('id');
        $result = Db::name('qrcode')
            ->where(array('id'=>$id))
            ->delete();
        if ($result) {
            $this->success_new('删除成功',url('operation/qrcode'));
        }else{
            $this->error_new('删除失败');
        }
    }

    /**
     * 添加二维码
     */
    public function qrcode_add(Request $request){
        if($request->isPost()){
            $data = $request->post();
            $result=Db::name('qrcode')->insert($data);
            if ($result) {
                $this->success_new('添加成功',url('operation/qrcode'));
            }else{
                $this->error_new('添加失败');
            }
        }
    }

    /**
     * 数据备份页面
     */
    public function data_bak(){
        $config['backupDir'] = 'database'. DS;
        $config['backupIgnoreTables'] = '';
        $backupDir = ROOT_PATH . 'public' . DS . $config['backupDir'];

        $backuplist = [];
        foreach (glob($backupDir . "*.zip") as $filename) {
            $time = filemtime($filename);
            $backuplist[$time] =
                [
                    'file' => str_replace($backupDir, '', $filename),
                    'date' => date("Y-m-d H:i:s", $time),
                    'size' => format_bytes(filesize($filename))
                ];
        }
        krsort($backuplist);
        return view('',[
            'backuplist'=>$backuplist,
        ]);
    }

    /**
     * 备份
     */
    public function backup()
    {
        $config['backupDir'] = 'database'. DS;
        $config['backupIgnoreTables'] = '';
        $backupDir = ROOT_PATH . 'public' . DS . $config['backupDir'];
        if ($this->request->isPost()) {
            $database = config('database');
            try {
                $backup = new Backup($database['hostname'], $database['username'], $database['database'], $database['password'], $database['hostport']);
                $backup->setIgnoreTable($config['backupIgnoreTables'])->backup($backupDir);
            } catch (Exception $e) {
                $this->error($e->getMessage());
                json_return(201,$e->getMessage());
            }
            json_return(200,'成功');
        }
    }




}